package com.jc.platform.songjiajia.service.base.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jc.platform.songjiajia.mapper.ISalesContractFileInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;




import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jc.platform.common.annotation.DataIsolation;
import com.jc.platform.common.constants.GlobalConstant;
import com.jc.platform.common.constants.StringPool;
import com.jc.platform.common.model.UserInfo;
import com.jc.platform.common.model.QueryParam;
import com.jc.platform.common.result.PageInfo;
import com.jc.platform.common.result.ResultCodeEnum;
import com.jc.platform.common.result.ResultModel;
import com.jc.platform.common.search.SqlUtils;
import com.jc.platform.common.utils.BeanUtil;
import com.jc.platform.common.utils.RequestUtil;
import com.jc.platform.songjiajia.vo.SalesContractFileInfoVO;
import com.jc.platform.songjiajia.entity.SalesContractFileInfoEntity;
import com.jc.platform.songjiajia.mapper.ISalesContractFileInfoMapper;
import com.jc.platform.songjiajia.service.base.impl.BaseServiceImpl;
import com.jc.platform.songjiajia.service.base.IBaseSalesContractFileInfoService;
import com.jc.platform.mybatis.utils.CollectCovertUtil;
import com.jc.platform.mybatis.utils.ConditionUtil;
import org.springframework.beans.factory.annotation.Autowired;

import com.jc.platform.redis.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.jc.platform.common.utils.CollectionUtil;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * ClassName AbstractSalesContractFileInfoServiceImpl.java
 * Description 销售拓展表
 *
 * @author 集团管理员
 * @date 2022/01/22
 */
@Slf4j
public abstract class AbstractSalesContractFileInfoServiceImpl extends BaseServiceImpl<SalesContractFileInfoVO> implements IBaseSalesContractFileInfoService
{
	private static final String KEY_PREFIX = "db_platformSongjiajia:t_salesContractFileInfo";

    @Autowired
    private RedisUtils<String, SalesContractFileInfoVO> redisUtils;
    @Autowired
    private ISalesContractFileInfoMapper salesContractFileInfoMapper;


    @Override
	public SalesContractFileInfoVO getBySalesContractIdExtend(Long salesContractId) {
        QueryWrapper<SalesContractFileInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sales_contract_id", salesContractId);
        SalesContractFileInfoEntity entity = salesContractFileInfoMapper.selectOne(queryWrapper);
        if (entity != null) {
            return entity.copyToVO();
        }
        return null;
    }

    @Override
    public Boolean delBySalesContractId(Long SalesContractId) {
        SalesContractFileInfoVO vo = getBySalesContractIdExtend(SalesContractId);
        if (vo != null) {
          delete(vo.getId());
        }
        return true;
    }




	@Override
	public ResultModel<SalesContractFileInfoVO> insert(SalesContractFileInfoVO salesContractFileInfoVO)
	{
		SalesContractFileInfoEntity entity = new SalesContractFileInfoEntity();
		entity.copyFormVO(salesContractFileInfoVO);
		salesContractFileInfoMapper.insert(entity);

		salesContractFileInfoVO.setId(entity.getId());
		delRedisKeyBatch(KEY_PREFIX);
		return ResultModel.success(salesContractFileInfoVO);
	}

	@Override
	public ResultModel<SalesContractFileInfoVO> update(SalesContractFileInfoVO salesContractFileInfo)
	{
		SalesContractFileInfoEntity entity = new SalesContractFileInfoEntity();
		entity.copyFormVO(salesContractFileInfo);
		salesContractFileInfoMapper.updateById(entity);



		salesContractFileInfo.setId(entity.getId());
		delRedisKeyBatch(KEY_PREFIX);
        delFromRedisAsync(entity.getId());
		entity = salesContractFileInfoMapper.selectById(entity.getId());
		return ResultModel.success(entity.copyToVO());
	}


    @Override
	public ResultModel<Boolean> delete(Long id)
	{
        
         int count = salesContractFileInfoMapper.deleteById(id);

		if (count > 0)
		{
			delRedisKeyBatch(KEY_PREFIX);
            delFromRedisAsync(id);
			return ResultModel.success(true);
		}
		else
		{
			return ResultModel.success(false);
		}
	}

    @Override
	public ResultModel<Boolean> deleteBatch(List<Long> ids)
	{
        if (CollectionUtil.isEmpty(ids)) {
			return ResultModel.success(true);
		}
        ids.forEach(id -> {
            salesContractFileInfoMapper.deleteById(id);
        });

        delRedisKeyBatch(KEY_PREFIX);
        return ResultModel.success(true);
	}




	@Override
	public Boolean checkNotExists(Long id, String property, String value)
	{
        QueryWrapper<SalesContractFileInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(BeanUtil.newInstance(SalesContractFileInfoEntity.class));
        queryWrapper.eq(SqlUtils.getColumnByProperty(property), value);

        if(id!=null && id>0){
            queryWrapper.ne(StringPool.ID, id);
        }


        return salesContractFileInfoMapper.selectCount(queryWrapper) <= 0;
	}

	@Override
	public ResultModel<SalesContractFileInfoVO> get(Long id)
	{
        String redisKey = RedisUtils.getRedisKey(KEY_PREFIX, id);
        SalesContractFileInfoVO salesContractFileInfoVO = getRedis(redisKey);
        if (salesContractFileInfoVO == null)
        {
            SalesContractFileInfoEntity entity = salesContractFileInfoMapper.selectById(id);
            if (entity == null){
                return ResultModel.failed();
            }
            salesContractFileInfoVO = entity.copyToVO();




            setRedis(redisKey, salesContractFileInfoVO);
        }
        return ResultModel.success(salesContractFileInfoVO);
	}

	@Override
	public ResultModel<List<SalesContractFileInfoVO>> find(QueryParam queryParam)
	{
		UserInfo userInfo = RequestUtil.getLoginUser();
		queryParam.getQuery().put("group_id",userInfo.getGroupId());
        String redisKey = RedisUtils.getRedisKey(KEY_PREFIX, "collection:find." + queryParam.toString());
        List<SalesContractFileInfoVO> list = getRedisForList(redisKey);
            if (list == null || list.isEmpty())
            {
                if (queryParam.getQuery().get("group_id") != null) {
                    queryParam.getQuery().put("sales_contract_file_info.group_id", queryParam.getQuery().get("group_id"));
                    queryParam.getQuery().remove("group_id");
                }
                if (queryParam.getQuery().get("org_id") != null) {
                    queryParam.getQuery().put("sales_contract_file_info.org_id", queryParam.getQuery().get("org_id"));
                    queryParam.getQuery().remove("org_id");
                }
                if (queryParam.getQuery().get("dept_id") != null) {
                    queryParam.getQuery().put("sales_contract_file_info.dept_id", queryParam.getQuery().get("dept_id"));
                    queryParam.getQuery().remove("dept_id");
                }
                QueryWrapper<SalesContractFileInfoVO> queryWrapper = ConditionUtil.getQueryWrapper(queryParam, SalesContractFileInfoVO.class);
                list = salesContractFileInfoMapper.findExtend(queryWrapper);

                if (list != null && !list.isEmpty())
                {
                    setRedis(redisKey,list);
                }
            }
        return ResultModel.success(list);
	}

	@Override
	public ResultModel<PageInfo<SalesContractFileInfoVO>> finds(QueryParam queryParam)
	{
		UserInfo userInfo = RequestUtil.getLoginUser();
		queryParam.getQuery().put("group_id",userInfo.getGroupId());

        String redisKey = RedisUtils.getRedisKey(KEY_PREFIX, "collection:finds." + queryParam.toString());
        PageInfo<SalesContractFileInfoVO> page = getRedisForPage(redisKey);
        if (page == null)
        {
            if (queryParam.getQuery().get("group_id") != null) {
                queryParam.getQuery().put("sales_contract_file_info.group_id", queryParam.getQuery().get("group_id"));
                queryParam.getQuery().remove("group_id");
            }
            if (queryParam.getQuery().get("org_id") != null) {
                queryParam.getQuery().put("sales_contract_file_info.org_id", queryParam.getQuery().get("org_id"));
                queryParam.getQuery().remove("org_id");
            }
            if (queryParam.getQuery().get("dept_id") != null) {
                queryParam.getQuery().put("sales_contract_file_info.dept_id", queryParam.getQuery().get("dept_id"));
                queryParam.getQuery().remove("dept_id");
            }
            QueryWrapper<SalesContractFileInfoVO> queryWrapper = ConditionUtil.getQueryWrapper(queryParam, SalesContractFileInfoVO.class);
            IPage<SalesContractFileInfoVO> iPage = salesContractFileInfoMapper.findExtends(ConditionUtil.getPage(queryParam), queryWrapper);
            page = new PageInfo<>();
            page.setList(iPage.getRecords());
            page.setPageNum(queryParam.getPageNum());
            page.setPageSize(queryParam.getPageSize());
            page.setTotal(iPage.getTotal());
            setRedis(redisKey,page);
        }
        return ResultModel.success(page);

	}

    @Override
	public ResultModel<Integer> count(QueryParam queryParam)
	{
        QueryWrapper<SalesContractFileInfoEntity> queryWrapper = ConditionUtil.getQueryWrapper(queryParam, SalesContractFileInfoEntity.class);
        Integer count = salesContractFileInfoMapper.selectCount(queryWrapper);
		return ResultModel.success(count);
	}







    private void delFromRedisAsync(Object id)
	{
        redisUtils.del(RedisUtils.getRedisKey(KEY_PREFIX, id));
	}

	private void delColRedis()
	{
		String keyPattern = RedisUtils.getRedisKey(KEY_PREFIX, "collection:*");
		Set<String> keys = redisUtils.keys(keyPattern);
		if (keys != null)
		{
			String[] keyArray = keys.toArray(new String[] {});
			redisUtils.del(keyArray);
		}
	}
}

